package com.sfang.common.util.string;

import java.util.Arrays;

public class VersionUtil {

    public static boolean valid(String versions, String currentVersion) {
        boolean valid = Arrays.stream(versions.split(",")).anyMatch(v -> {
            Integer minVersion = 0,
                    maxVersion = 10000,
                    curVersion = convert(currentVersion);

            if (StringUtils.isNotEmpty(v) && !v.equals("0")) {
                String[] versionRange = v.split("-");

                if (versionRange.length > 0)
                    minVersion = convert(versionRange[0]);

                if (versionRange.length > 1)
                    maxVersion = convert(versionRange[1]);

                if (v.indexOf("-") == -1 && versionRange.length > 0)
                    maxVersion = convert(versionRange[0]);

            }

            return curVersion >= minVersion && curVersion <= maxVersion;
        });

        return valid;
    }

    private static Integer convert(String value) {
        Integer version = Integer.valueOf(value.replace(".", ""));

        if (version < 1000)
            version = version * (4 - version.toString().length()) * 10;

        return version;
    }

}
